<template>
  <div class="succ-box">
    <div class="news-title">
      <el-icon><Promotion /></el-icon>
      <div class="title">社区留言</div>
    </div>

    <div class="rev-btn">
      <el-popover
        :visible="visible"
        placement="bottom"
        :width="200"
        content="请输入内容..."
      >
        <template #reference>
          <el-button
            type="primary"
            @click="subMessage"
            @keydown.enter="subMessage"
            >发表留言<el-icon><EditPen /></el-icon
          ></el-button>
        </template>
      </el-popover>
    </div>
    <div class="rev-box">
      <el-input
        rows="5"
        type="textarea"
        v-model="textarea"
        placeholder="欢迎留言..."
      />
    </div>

    <div class="news-list">
      <div class="box-left">
        <div class="succ-item" v-for="(item, index) in dissList" :key="index">
          <div class="item-left">
            <img :src="item.avater" class="imgs" />
          </div>

          <div class="item-right">
            <span>{{ item.userName }}</span>
            <div class="content">
              <div class="infos">
                {{ item.content }}
              </div>
              <div class="time">{{ utils.day(item.create_time) }}</div>
            </div>
          </div>
        </div>
        <div @click="getMore" class="getMore" v-show="total > 10">
          <div v-if="!loading" class="more">更多评论</div>
          <div v-if="loading">没有更多评论</div>
        </div>
      </div>
      <Searchbar />
    </div>
  </div>
</template>

<script lang="ts" setup>
import Searchbar from "@/components/common/tabbar/searchbar.vue";
import { getCurrentInstance, nextTick, onMounted, ref } from "vue";
import utils from "@/utils/utils";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
import { getReviews, query } from "@/server/reviews";

const userInfos = ref<any>({});
const router = useRouter();
const { proxy }: any = getCurrentInstance();
const dissList = ref<any>([]);
const total = ref(0);

onMounted(async () => {
  await getDissList();
  userInfos.value = JSON.parse(
    window.sessionStorage.getItem("Infos") || "null"
  );
});
const getDissList = () => {
  getReviews().then((res: any) => {
    dissList.value = res.list;
    total.value = res.total;
  });
};
const textarea = ref("");
// 显示发表评论按钮的提示
const visible = ref(false);
const subMessage = utils.throttle(async () => {
  if (userInfos.value == null) {
    ElMessageBox.alert("请先登录", {
      confirmButtonText: "OK",
      callback: () => {
        router.push("/ulogin");
        window.sessionStorage.clear()
      },
    });
  } else {
    if (textarea.value.trim() == "") {
      visible.value = !visible.value;
      setTimeout(() => {
        visible.value = !visible.value;
      }, 1500);
      textarea.value = "";
    } else {
      proxy.$http
        .post("/user/addDis?id=" + userInfos.value.id, {
          userName: userInfos.value.userName,
          avater: userInfos.value.avater,
          content: textarea.value,
          uid: userInfos.value.id,
        })
        .then((res: any) => {
          ElMessage({
            message: res,
            type: "success",
          });
          textarea.value = "";
          getDissList();
        });
    }
  }
}, 1000);

const loading = ref(false);
const page = ref(1);
//加载更多评论
const getMore = () => {
  if (dissList.value.length / total.value == 1) return (loading.value = true);
  query.pageNum = ++page.value;
  getReviews().then((res: any) => {
    dissList.value = dissList.value.concat(res.list);
  });
};
</script>
<style scoped lang="less">
.succ-box {
  width: 80%;
  margin: auto;
  padding-top: 10px;
  .news-title {
    display: flex;
    align-items: center;
    margin: 10px 0;
    font-size: 24px;
    padding: 10px;
    border-radius: 10px;
    background-color: #eee;
    .title {
      font-weight: bolder;
    }
  }
  .rev-btn {
    margin: 5px 0;
  }
  .rev-box {
    width: 65%;
    margin: 10px 0;
  }
  .news-list {
    display: flex;
    .box-left {
      padding: 10px 0;
      padding-top: 0;
      border-radius: 10px;
      width: 70%;
      margin-right: 10px;
      .succ-item {
        display: flex;
        margin-bottom: 15px;
        padding: 10px;
        // border-radius: 10px;
        // background-color: #eee;
        border-bottom: 1px solid #e5e9ef;
        .item-left {
          width: 60px;
          height: 60px;
          .imgs {
            border-radius: 50px;
            width: 100%;
            height: 100%;
          }
        }
        .item-right {
          display: flex;
          flex: 1;
          flex-direction: column;
          margin-left: 10px;
          span {
            font-weight: bolder;
          }
          .content {
            height: 100%;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
          }
          .time {
            font-size: 12px;
          }
        }
      }
    }
  }
  .getMore {
    font-size: 12px;
    color: gray;
    display: flex;
    align-items: center;
    justify-content: center;
    .more {
      font-size: 16px;
    }
    .more:hover {
      cursor: pointer;
      color: rgb(76, 76, 227);
    }
  }
}
</style>
